package com.fjnldx.mes.service.alarm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjnldx.mes.domain.domainReq.alarm.EditAlarmRuleReq;
import com.fjnldx.mes.domain.pojo.alarm.AlarmRules;
import com.fjnldx.mes.mapper.alarm.AlarmRuleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 告警规则服务
 */
@Service
public class AlarmRuleService {
    @Resource
    private AlarmRuleMapper alarmRuleMapper;

    /**
     * 更新告警规则
     *
     * @param param 更新参数
     * @throws Exception 未知异常信息
     */
    public void UpdateAlarmRule(Integer alarmId, List<EditAlarmRuleReq> param) throws Exception {
        try {
            List<AlarmRules> rules = getAlarmRules(alarmId);
            for (EditAlarmRuleReq editInfo : param) {
                AlarmRules rule = null;
                List<AlarmRules> filterRules = rules.stream().filter(x -> x.getId() == (editInfo.getId() == null ? 0 : editInfo.getId()))
                        .collect(Collectors.toList());
                if (filterRules.size() > 0) rule = filterRules.get(0);
                if (rule == null) {
                    //添加
                    rule = new AlarmRules();
                    rule.setAlarmId(alarmId);
                    rule.setName(editInfo.getName());
                    rule.setLevel(editInfo.getLevel());
                    rule.setMonitorType(editInfo.getMonitoringType());
                    rule.setMonitorValue(editInfo.getMonitoringValue());
                    rule.setTriggerType(editInfo.getTriggerType());
                    rule.setTriggerValue(editInfo.getTriggerValue());
                    rule.setRelieveType(editInfo.getRelieveType());
                    rule.setRelieveValue(editInfo.getRelieveValue());
                    rule.setCreateTime(LocalDateTime.now());
                    alarmRuleMapper.insert(rule);
                } else {
                    //更新
                    rule.setName(editInfo.getName());
                    rule.setLevel(editInfo.getLevel());
                    rule.setMonitorType(editInfo.getMonitoringType());
                    rule.setMonitorValue(editInfo.getMonitoringValue());
                    rule.setTriggerType(editInfo.getTriggerType());
                    rule.setTriggerValue(editInfo.getTriggerValue());
                    rule.setRelieveType(editInfo.getRelieveType());
                    rule.setRelieveValue(editInfo.getRelieveValue());
                    alarmRuleMapper.updateById(rule);
                }
            }
            //删除
            List<Integer> paramIds = param.stream().map(EditAlarmRuleReq::getId).collect(Collectors.toList());
            List<Integer> currentIds = rules.stream().map(AlarmRules::getId).collect(Collectors.toList());
            List<Integer> deleteIds = currentIds.stream().filter(id -> !paramIds.contains(id)).collect(Collectors.toList());
            if (deleteIds.size() > 0)
                alarmRuleMapper.deleteBatchIds(deleteIds);
        } catch (Exception e) {
            throw new Exception("更新告警规则失败");
        }
    }

    /**
     * 获取告警规则
     *
     * @param id 告警配置标识
     * @return 告警规则集合响应值
     */
    public List<AlarmRules> getAlarmRules(Integer id) throws Exception {
        try {
            //查询配置规则
            QueryWrapper<AlarmRules> alarmRulesQueryWrapper = new QueryWrapper<>();
            alarmRulesQueryWrapper.eq("alarmId", id);
            return alarmRuleMapper.selectList(alarmRulesQueryWrapper);
        } catch (Exception e) {
            throw new Exception();
        }
    }
}
